layui.use(['layer', 'jquery', 'form'], function() {

    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    let token = localStorage.getItem('token'); //获取用户token
    var parentlist = $(parent.document).find('#books'); //获取父页面tbody
    //添加操作
    form.on('submit(addBtn)', function(data) {
        let field = data.field; //获取表单数据
        console.log(field.id);
        $.ajax({
            method: 'POST',
            url: '/my/article/addcates',
            data: field,
            success: function(result) {
                if (result.status == 0) {
                    console.log(result);

                    layer.msg('添加成功！', {
                            time: 500 //2秒关闭（如果不配置，默认是3秒）
                        },
                        function() {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭 

                            $('#books tr', parent.document).remove();
                            $.ajax({
                                method: 'GET',
                                url: '/my/article/cates',
                                success: function(result) {
                                    if (result.status == 0) {
                                        console.log(result);

                                        let trs = '';
                                        result.data.forEach(function(item) {
                                            let tr =
                                                `<tr><td id='booksid' name='Id' style='display:none'>${item.Id}</td><td>${item.name}</td><td>${item.alias}</td>
                                                               <td><button type="button" class="layui-btn layui-btn-normal editbooks">编辑</button><button type="button" class="layui-btn layui-btn-danger delbooks">删除</button></td></tr>`;

                                            trs += tr;
                                            // parent.document.location.reload();

                                        })
                                        $('#books', parent.document).append(
                                            trs);
                                    }
                                }
                            })


                        });
                }

            }

        })
        return false; //阻止表单提交
    });
});
